import pygame
import random

pygame.init()
WHITE = (255, 255, 255)
screen = pygame.display.set_mode((450,800), 0, 32)
plane = pygame.image.load('plane.png')

class Enemy:
    def restart(self):
        self.x = random.randint(50, 400)
        self.y = random.randint(-200, -50)
        self.speed = random.random() * 2 + 0.5
        
    def __init__(self):
        self.restart()
        self.image = pygame.image.load('enemy.png').convert_alpha()

    def move(self):
        if self.y < 800:
            self.y += self.speed
        else:
            self.restart()

class Plane:
    def restart(self):
        self.x = 200
        self.y = 600
        
    def __init__(self):
        self.restart()
        self.image = pygame.image.load('plane.png').convert_alpha()

    def move(self):
        x, y = pygame.mouse.get_pos()
        x-= self.image.get_width() / 2
        y-= self.image.get_height() / 2
        self.x = x
        self.y = y

plane = Plane()
enemies = []
for i in range(5):
    enemies.append(Enemy())

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()
    
    # 绘制背景
    screen.fill(WHITE)
    # 绘制敌机
    for e in enemies:
        e .move()
        screen.blit(e.image, (e.x,e.y))
    # 绘制玩家
    plane.move()
    screen.blit(plane.image, (plane.x, plane.y))

    pygame.display.update()